package com.xywang.service.user;

import com.xywang.dao.user.UserDao;
import com.xywang.pojo.User;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDaoImpl;
    public void setUserDaoImpl(UserDao userDaoImpl) {
        this.userDaoImpl = userDaoImpl;
    }

    @Override
    public User login(String userCode, String userPassword) {
        User loginUser = userDaoImpl.getLoginUser(userCode);
        // 密码判断，如果密码不一致，则设user为空，即登录失败
        if(loginUser!=null){
            if(!loginUser.getUserPassword().equals(userPassword)){
                loginUser = null;
            }
        }
        return loginUser;
    }

    @Override
    public boolean updatePwd(int id, String password) {
        boolean flag = false;
        int i = userDaoImpl.updatePwd(id, password);
        if(i>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public int getUserCount(String username, int userRole) {
        return userDaoImpl.getUserCount(username,userRole);
    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        //在mysql数据库中，分页使用 limit startIndex，pageSize ;
        // 当前页=（当前页-1）*页而大小
        // 页1    0,5  01234
        // 页2    5,5  56789
        // 页3    10,5  10~
        // currentPageNo 当前页
        // pageSize 页面大小
        // 当前页 1 ，则为 limit 0,pageSize;
        // 当前页 2 ，则为 limit 5,pageSize;
        currentPageNo = (currentPageNo - 1) * pageSize;
        return userDaoImpl.getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
    }

    @Override
    public boolean addUserService(User user) {
        boolean flag = false;
        int i = userDaoImpl.addUserDao(user);
        if(i>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public User selectUserByCode(String userCode) {
        return userDaoImpl.getLoginUser(userCode);
    }

    @Override
    public boolean deleteUserService(int id) {
        boolean flag = false;
        int i = userDaoImpl.deleteUser(id);
        if(i>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public User getUserByIdService(int id) {
        return userDaoImpl.getUserById(id);
    }

    @Override
    public boolean updateUserService(User user) {
        boolean flag = false;
        int i = userDaoImpl.updateUser(user);
        if(i>0){
            flag = true;
        }else{
            System.out.println("updateUserService->"+i);
        }
        return flag;
    }
}
